package com.maxvision.mybatis.controller;

import com.maxvision.mybatis.converter.StringFileConverter;
import com.maxvision.mybatis.service.ImageService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.lang.reflect.Field;
import java.util.concurrent.Future;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * @author HuangRongFu
 * 2022/12/9 星期五21:51
 */

@RestController
@RequestMapping("/image")
@Log4j2
public class ImageController extends BaseController{

    @Autowired
    private ImageService imageService;
    
    @Autowired
    private StringFileConverter converter;
    
    @PostMapping("/callableSubmit")
    public ResponseEntity importImages(@RequestParam(value = "images") MultipartFile[] multipartFiles){

        long start = System.currentTimeMillis();

        ResponseEntity responseEntity = imageService.importImagesByCallableSubmit(multipartFiles);

        long end = System.currentTimeMillis();
        System.out.println("导入图片耗时: " + (end - start) + "ms");
        return responseEntity;
    }

    @PostMapping("/runnableSubmit")
    public ResponseEntity importImagesByRunnableSubmit(@RequestParam(value = "images") MultipartFile[] multipartFiles){

        long start = System.currentTimeMillis();

        ResponseEntity responseEntity = imageService.importImagesByRunnableSubmitCountDownLatch(multipartFiles);

        long end = System.currentTimeMillis();
        System.out.println("导入图片耗时: " + (end - start) + "ms");
        return responseEntity;
    }

    @PostMapping("/runnableExecute")
    public ResponseEntity importImagesByRunnableExecute(@RequestParam(value = "images") MultipartFile[] multipartFiles){

        long start = System.currentTimeMillis();

        ResponseEntity responseEntity = imageService.importImagesByRunnableSubmit(multipartFiles);

        long end = System.currentTimeMillis();
        System.out.println("导入图片耗时: " + (end - start) + "ms");
        return responseEntity;
    }


    @PostMapping("/importList")
    public ResponseEntity importImageList(@RequestPart(value = "images") MultipartFile[] multipartFiles,
                                          @RequestParam("name") String name){

        long start = System.currentTimeMillis();

        log.info("接收到name参数为:{}", name);
        ResponseEntity responseEntity = imageService.importImagesForList(multipartFiles);

        long end = System.currentTimeMillis();
        System.out.println("导入图片耗时: " + (end - start) + "ms");
        return responseEntity;
    }
    
    @PostMapping("/converter")
    public ResponseEntity converterFile(){

        File file = new File("C:\\Users\\PC\\Desktop\\1.jpg");
        String convert = converter.convert(file);
        return new ResponseEntity(convert, HttpStatus.OK);
    }
    
    @GetMapping("/async")
    public ResponseEntity async() throws Exception {
        long start = System.currentTimeMillis();
       
        System.out.println("任务开始执行");
        Future<String> async = imageService.async();
 
        long end = System.currentTimeMillis();
        System.out.println("调用异步任务耗时: " + (end - start) + "ms");
        return new ResponseEntity("完成", HttpStatus.OK);
    }


}
